package com.rongke.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Address;
import com.rongke.redis.CacheUtil;
import com.rongke.service.AddressService;
import com.rongke.service.MuserService;
import com.rongke.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @AddressController
 * @地址Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/address")
@Transactional
public class AddressController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private AddressService addressService;
    @Autowired
    private MuserService muserService;
    @Autowired
    private CacheUtil cacheUtil;

    /**
     * @添加地址收货
     * @param address
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/addAddress", method = RequestMethod.POST)
    public JsonResp addAddress(@RequestBody Address address,String token){
        log.debug("添加地址收货");
        if(!cacheUtil.hasKey(token)){
            return JsonResp.fa(muserService.LOGINSTR);
        }
        Long userId=Long.valueOf(cacheUtil.get(token));
        String tag=address.getTag();
        if("1".equals(tag)){
            EntityWrapper<Address> ew=new EntityWrapper<>();
            ew.eq("tag","1");//1表示收货地址默认
            ew.eq("muser_id",userId);
            Address addressOld=addressService.selectOne(ew);
            if(addressOld!=null){
                addressOld.setTag("2");
                addressService.updateById(addressOld);
            }
;        }
        address.setMuserId(userId);

        address.setProvince(AddressController.convertProvince(address.getProvince()));//转化处理带"省"、"市"、"自治区"

        addressService.insert(address);
        return JsonResp.ok(address);
    }

    /**
     * @修改地址
     * @param address
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateAddress(@RequestBody Address address,String token){
        log.debug("修改地址");
        if(!cacheUtil.hasKey(token)){
            return JsonResp.fa(muserService.LOGINSTR);
        }
        if(address.getId()==null || "".equals(address.getId())){
            return JsonResp.fa("参数错误");
        }
        Address addressOld=addressService.selectById(address.getId());
        addressOld.setTag(address.getTag());
        addressOld.setPhone(address.getPhone());
        addressOld.setName(address.getName());
        addressOld.setProvince(address.getProvince());
        addressOld.setCity(address.getCity());
        addressOld.setArea(address.getArea());
        addressOld.setAreaCode(address.getAreaCode());
        addressOld.setAddressDetail(address.getAddressDetail());
        addressService.updateById(addressOld);
        return JsonResp.ok(addressOld);
    }

    /**
     * @地址删除
     * @param id 地址id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/delAddress", method = RequestMethod.GET)
    public JsonResp delAddress(Long id,String token){
        log.debug("地址删除");
        if(!cacheUtil.hasKey(token)){
            return JsonResp.fa(muserService.LOGINSTR);
        }
        addressService.deleteById(id);
        return JsonResp.ok("","200");
    }



    /**
     * @根据id查找地址
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectAddress(Long id){
        log.debug("查找地址");
        Address address = addressService.selectById(id);
        return JsonResp.ok(address);
    }

    /**
     * @获取某一用户的默认收货地址
     * @param token
     * @return
     */
    @RequestMapping(value = "/selectDefaultAddressByUserId",method=RequestMethod.GET)
    public JsonResp selectDefaultAddressByUserId(String token){
        log.debug("获取某一用户的默认收货地址");
        if(!cacheUtil.hasKey(token)){
            return JsonResp.fa(muserService.LOGINSTR);
        }
        String userId=cacheUtil.get(token);
        EntityWrapper<Address> ew =new EntityWrapper<>();
        //收货地址的默认地址的tag为1
        ew.eq("muser_id",userId).eq("tag",'1');
        Address address=addressService.selectOne(ew);
        if(address!=null){
            return JsonResp.ok(address);
        }else {
            return JsonResp.ok();
        }

    }

    /**
     * @获取某一用户的收货地址列表"
     * @param userId
     * @return
     */
    @RequestMapping(value = "/selectAddressListByUserId",method=RequestMethod.GET)
    public JsonResp selectAddressListByUserId(String userId){
        log.debug("获取某一用户的收货地址列表");
        if(StringUtil.isEmpty(userId))
            return JsonResp.toFail("用户id参数不能为空！");
        EntityWrapper<Address> ew =new EntityWrapper<>();
        //收货地址的tag为1和2
        ew.where("tag in ({0},{1})",'1','2').eq("muser_id",userId);
        List<Address> addressList=addressService.selectList(ew);
        return JsonResp.ok(addressList);
    }

    /**
     * @获取会员注册地址
     * @param userId
     * @return
     */
    @RequestMapping(value = "/selectMemberAddress",method = RequestMethod.GET)
    public JsonResp selectMemberAddress(String userId){
        log.debug("获取会员注册地址");
        if(StringUtil.isEmpty(userId))
            return JsonResp.toFail("用户id参数不能为空！");
        EntityWrapper<Address> ew =new EntityWrapper<>();
        ew.eq("muser_id",userId).eq("tag",'0');
        Address address=addressService.selectOne(ew);
        return JsonResp.ok(address);
    }

    public static void main(String[] args) {
        String province="新疆维吾尔自治区";
        Address address=new Address();
        address.setProvince(AddressController.convertProvince(province));
        System.out.println(address);
    }

    /**
     * 处理掉"省"、"市"、"自治区"等后缀
     * @param province
     * @return
     */
    public static String convertProvince(String province){
        if (province.contains("省") || province.contains("市")) {
            province = province.substring(0, province.length() - 1);
        } else if (province.equals("内蒙古自治区")) {
            province = "内蒙古";
        } else if (province.equals("西藏自治区")) {
            province = "西藏";
        }else if (province.equals("新疆维吾尔自治区")) {
            province = "新疆";
        }else if (province.equals("宁夏回族自治区")) {
            province = "宁夏";
        }else if (province.equals("广西壮族自治区")) {
            province = "广西";
        }
        return province;
    }
}
